<?php

class PathUtil {

	public static function getRuntimePath($taskid, $jobid, $date, $isHadoop = 0) {
		$date = DateHelper::makePathDate(strtotime($date));
		if ($isHadoop == 0) {
			$path = RUNTIME_PATH . "/$date/{$jobid}_$taskid";
		}
		else {
			$path = HADOOP_RUNTIME_PATH . "/$date/{$jobid}_$taskid";
		}
		return $path;
	}
	
	public static function getHadoopClientPath($clusterId) {
		$hadoopClient = HADOOP_CLIENT . "/" . $clusterId . "/hadoop/hadoop/bin/hadoop";
		return $hadoopClient;
	}

	public static function getHadoopSiteXmlPath($clusterId) {
		$hadoopSiteXml = HADOOP_CLIENT . "/" . $clusterId . "/hadoop/hadoop/conf/hadoop-site.xml";
		return $hadoopSiteXml;
	}

	public static function getHadoopContribPath($clusterId) {
		$hadoopContrib = HADOOP_CLIENT . "/" . $clusterId . "/hadoop/hadoop/contrib/hod/conf/hodrc";
		return $hadoopContrib;
	}

	public static function getHadoopConfPath($clusterId) {
		$hadoopSiteXml = HADOOP_CLIENT . "/" . $clusterId . "/hadoop/hadoop/conf";
		return $hadoopSiteXml;
	}

	public static function getFTPPath($machine, $path, $userName, $password) {
		$machine = trim($machine);
		$path = trim($path);
		if(isset($userName) && strlen($userName) > 0) {
			return 'ftp://' . $userName . ':' . $password . '@' . $machine . '/' . $path;
		}
		else {
			return 'ftp://' . $machine . $path;
		}
	}
	
	public static function hdfsFileExists($hadoopClient, $file) {
		system($hadoopClient. '/bin/hadoop dfs -test -e '. $file, $retVal);
		return $retVal == 0;
	}
	
	public static function hdfsDirExists($hadoopClient, $dir) {
		system($hadoopClient. '/bin/hadoop dfs -test -d '. $dir, $retVal);
		return $retVal == 0;
	}
}
